package com.njwb.Swing.Login;

import javax.swing.*;
import java.awt.*;

public class LoginView extends JFrame {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JComboBox<String> roleBox;
    private JButton loginButton;

    public LoginView() {
        setSize(700, 440);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("我的系统");

        setResizable(false);
        setUndecorated(false);


        // 背景图片
        ImageIcon originalIcon = new ImageIcon(getClass().getResource("/com/njwb/Util/images/login_box.jpg"));
        Image scaledImage = originalIcon.getImage().getScaledInstance(700, 440, Image.SCALE_SMOOTH);
        ImageIcon bgIcon = new ImageIcon(scaledImage);

        JLabel bgLabel = new JLabel(bgIcon);
        bgLabel.setBounds(0, 0, 700, 400);
        setContentPane(bgLabel);
        bgLabel.setLayout(null);

        // 用户名
        JLabel usernameLabel = new JLabel("用户名:");
        usernameLabel.setBounds(200, 180, 80, 25);
        usernameLabel.setForeground(Color.WHITE);
        bgLabel.add(usernameLabel);

        usernameField = new JTextField();
        usernameField.setBounds(280, 180, 160, 25);
        usernameField.setForeground(Color.BLACK);
        usernameField.setBorder(BorderFactory.createLineBorder(Color.WHITE));
        bgLabel.add(usernameField);

        // 密码
        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setBounds(200, 220, 80, 25);
        passwordLabel.setForeground(Color.WHITE);
        bgLabel.add(passwordLabel);

        passwordField = new JPasswordField();
        passwordField.setBounds(280, 220, 160, 25);
        passwordField.setForeground(Color.BLACK);
        passwordField.setBorder(BorderFactory.createLineBorder(Color.WHITE));
        bgLabel.add(passwordField);

        // 身份
        JLabel roleLabel = new JLabel("身份:");
        roleLabel.setBounds(200, 260, 80, 25);
        roleLabel.setForeground(Color.WHITE);
        bgLabel.add(roleLabel);

        roleBox = new JComboBox<>(new String[]{"普通员工", "经理"});
        roleBox.setBounds(280, 260, 160, 25);
        roleBox.setForeground(Color.BLACK);
        roleBox.setFont(new Font("微软雅黑", Font.BOLD, 14));
        bgLabel.add(roleBox);

        // 登录按钮
        loginButton = new JButton("登录");
        loginButton.setBounds(280, 300, 160, 35);
        loginButton.setForeground(Color.WHITE);
        loginButton.setBackground(new Color(0, 0, 0, 200));
        loginButton.setFocusPainted(false);
        loginButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
        loginButton.setBorder(BorderFactory.createLineBorder(Color.WHITE, 2));
        bgLabel.add(loginButton);
    }

    public String getUsername() {
        return usernameField.getText().trim();
    }

    public String getPassword() {
        return new String(passwordField.getPassword()).trim();
    }

    public int getRole() {
        return roleBox.getSelectedIndex();
    }

    public void addLoginListener(java.awt.event.ActionListener listener) {
        loginButton.addActionListener(listener);
    }

    public void showMessage(String message, String title, int type) {
        JOptionPane.showMessageDialog(this, message, title, type);
    }
}
